<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class periode_model extends CI_Model {

	function __construct()
	{
		parent::__construct();
		$this->CI = get_instance();
	}
	
	function insert($data){
		$this->db->insert('periode',$data);
	}
	
	//list flexy
	function get_data_periode(){
		//Select table name
		$table_name = "periode";
		
		//Build contents query
		$this->db->select('*')->from($table_name);
		$this->CI->flexigrid->build_query();
		
		//Get contents
		$return['records'] = $this->db->get();
		
		//Build count query
		$this->db->select('*')->from($table_name);
                $this->CI->flexigrid->build_query(FALSE);
		
		$return['record_count'] = $this->db->count_all_results();
	
		//Return all
		return $return;
	}
	
	
	function getAllGrid($start,$limit,$sidx,$sord,$where){
		$this->db->select('ID_PESERTA','NAMA_PESERTA');
		$this->db->limit($limit);
		if($where != NULL)$this->db->where($where,NULL,FALSE);
		$this->db->order_by($sidx,$sord);
		$query = $this->db->get('peserta',$limit,$start);

		return $query->result();
	}
	
	
	function delete($id){
		$this->db->where('ID_periode', $id);
		$this->db->delete('periode'); 
	}
	
	function selectone($id){
		$this->db->select("*");
		$this->db->from('periode');
		$this->db->where('ID_periode',$id);
		$query = $this->db->get();
		return $query;
	}

	
	
	function update($id, $data){
		$this->db->where('ID_periode',$id);
		$this->db->update('periode',$data);
	}
	
	function get_period() {
		$this->db->select('*');
		$this->db->from('periode');
		return $this->db->get()->result();
	}
	
	
	function active_period() {
		$this->db->select('*');
		$this->db->from('periode');
		$this->db->where('AKTIF = 1');
		return $this->db->get()->result();
	}
	
	
	//-----------------Start Update Status Aktif periode-------------------//
	function set_status_nol($data){
		$this->db->where('AKTIF', 1);
		$this->db->update('periode', $data);
	}
	
	function set_status_satu($id, $data){
		$this->db->where('ID_periode', $id);
		$this->db->update('periode', $data);
	}
	//-----------------End Update Status Aktif periode-------------------//
	
	
	
	function get_data_buku_l($id_periode){
		$this->db->select('*');
		$this->db->from('kelas_santri as ks');//, kelas as k, kelas_santri as ks, ustad as u, santri as s');
		$this->db->join('santri as s', 'ks.id_santri = s.id_santri', 'left');
		$this->db->join('kelas as k', 'ks.id_kelas = k.id_kelas', 'left');
		$this->db->join('ustad as u', 'k.id_ustad = u.id_ustad', 'left');
		//$this->db->join('kelas_santri as ks', 'ks.id_kelas = k.id_kelas', 'left');
		
		//$this->db->join('kelas_santri as ks', 'p.id_periode = k.id_periode', 'left');
		
		$this->db->where('k.id_periode', $id_periode);
		$this->db->where('s.id_jk', 'Laki-Laki');
		$this->db->order_by('s.nama_santri', 'asc');
		$query = $this->db->get();
        return $query;
	}
	
	
	
	function get_data_buku_p($id_periode){
		$this->db->select('*');
		$this->db->from('kelas_santri as ks');//, kelas as k, kelas_santri as ks, ustad as u, santri as s');
		$this->db->join('santri as s', 'ks.id_santri = s.id_santri', 'left');
		$this->db->join('kelas as k', 'ks.id_kelas = k.id_kelas', 'left');
		$this->db->join('ustad as u', 'k.id_ustad = u.id_ustad', 'left');
		//$this->db->join('kelas_santri as ks', 'ks.id_kelas = k.id_kelas', 'left');
		
		//$this->db->join('kelas_santri as ks', 'p.id_periode = k.id_periode', 'left');
		
		$this->db->where('k.id_periode', $id_periode);
		$this->db->where('s.id_jk', 'Perempuan');
		$this->db->order_by('s.nama_santri', 'asc');
		$query = $this->db->get();
        return $query;
	}
	
}

?>